package com.example.demo.web.controller;

import com.example.demo.constant.RequestUrl;
import com.example.demo.param.LoginParam;
import com.example.demo.service.OperatorService;
import com.example.demo.util.IPUtil;
import com.example.demo.util.NumberUtil;
import com.example.demo.vo.ApiResult;
import com.example.demo.vo.LoginVO;
import com.example.demo.web.api.LoginApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = RequestUrl.API)
public class LoginController implements LoginApi {

    @Autowired
    private OperatorService operatorService;

    @Override
    @PostMapping(value = RequestUrl.LOGIN)
    public ApiResult<LoginVO> login(@RequestBody LoginParam loginParam) throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

        return operatorService.login(loginParam, IPUtil.StringToLong(IPUtil.getIpAddress(request)));
    }
}
